package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public int addBed(BedDto bedDto) {
        //TODO 是否需要做重名判断
        Bed bed = BeanUtil.copyProperties(bedDto, Bed.class);
        bed.setBedStatus(0) ; // 默认床位状态为 未入住0
        int num = bedMapper.countByBedNumber(bed.getBedNumber());
        if (num>0){
            throw new BaseException(BasicEnum.BED_INSERT_FAIL_DUPLICATE_BED_NUMBER);
        }
        return bedMapper.insert(bed);
    }

    @Override
    public int deleteById(Long id) {

        //TODO 该床位有人在使用则不能删除？？？


        return  bedMapper.deleteById(id);
    }

    /**
     * 更新现有的床位
     * @param bedDto 床位数据传输对象
     */
    @Override
    public void updateBed(BedDto bedDto) {
        BedVo bedVo = selectBedById(bedDto.getId());
        bedVo.setSort(bedDto.getSort());
        bedVo.setBedNumber(bedDto.getBedNumber());
        if (ObjectUtil.isNotEmpty(bedDto.getBedStatus())) {
            bedVo.setBedStatus(bedDto.getBedStatus());
        }
        Bed bed = BeanUtil.toBean(bedVo, Bed.class);
        bed.setCreateTime(LocalDateTime.now());
        bed.setCreateBy(1L);
        bedMapper.updateBed(bed);
    }

    @Override
    public BedVo selectBedById(Long id) {
        return BeanUtil.toBean(bedMapper.selectBedById(id), BedVo.class);
    }


}

